<div class="fsh-rightPanel">
    <div class="layui-anim layui-anim-upbit">
        <div class="layui-form-item" id="list_form_user">
            <div class="layui-inline">
                <select id="dutySelect" name="duty_id" class="layui-input">
                </select>
            </div>
            <div class="layui-inline">
                <input type="text" name="nickname" placeholder="请输入用户名称" autocomplete="off" class="layui-input">
            </div>
            <button class="layui-btn btn-primary" id="search_user">搜索</button>
            <div class="layui-btn-container" style="text-align: right">
                <button class="layui-btn btn-primary" id="user_add">添加教师</button>
            </div>
        </div>
        <table id="list_table_user" class="layui-hide" lay-filter="mainList_schoolUser"></table>
    </div>
</div>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="del">删除</button>
    </div>
</script>
<script>
    layui.use(['layer', 'table', 'form'], function () {
        var form = layui.form;
        var table = layui.table;
        var tableObj_user = table.render({
            elem: '#list_table_user',
            url: context + '/api/sysuser/getlist?schoolCode='+ '<%=schoolCode%>',
            request: {
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            align: 'center'
            , cols: [[ //表头
                // {type: 'checkbox', fixed: 'left'},
                {
                    field: 'photo', title: '头像', align: 'center', templet: function (d) {
                        return d.photo !== undefined ? '<div><img src="' + d.photo + '" style="height:100px;width:100px;"/></div>' : '';
                    }
                },
                {field: 'username', title: '账号', align: 'center'},
                {field: 'nickname', title: '姓名', align: 'center'},
                {
                    title: '性别', align: 'center', templet: function (d) {
                        return d.sex === 0 ? '男' : '女';
                    }, width: 80
                },
                {
                    title: '用户类型', align: 'center', width: 120, templet: function (d) {
                        return userType[(d.role_id)];
                    }
                },
                {
                    title: '职位', align: 'center', width: 120, templet: function (d) {
                        return getUserDuty()[d._eduSchoolUser.duty_id]||'';
                    }
                },
                {field: 'email', title: '邮箱', align: 'center'},
                {field: 'createDate', title: '创建时间', align: 'center'},
                {
                    field: 'status', title: '状态', width: 100, align: 'center', templet: function (d) {
                        var s = d.status;
                        if (s === 0) {
                            return '正常';
                        } else if (s === 1) {
                            return '锁定';
                        } else if (s === 2) {
                            return '已解锁';
                        } else {
                            return '永久锁定';
                        }
                    }
                },
                {
                    field: 'lock', title: '操作', width: 240, align: 'center', unresize: true,
                    templet: function (d) {
                        return  '<a href="javascript:;" class="font-primary" lay-event="updateDuty">修改职位</a>'
                            + '<a href="javascript:;" class="font-primary" lay-event="status">状态</a>'
                            + '<a href="javascript:;" class="font-primary" lay-event="update">修改</a>'
                            + '<a href="javascript:;" class="font-primary" lay-event="del">删除</a>';

                    }
                }
            ]]
            , page: true

        });

        // 检索
        $("#search_user").click(function () {
            reloadTable(tableObj_user,"list_form_user");
        });

        // 添加
        $("#user_add").click(function () {
            showDialog({
                title: '新建用户'
                , template: context + '/views/system/school/template/addSchoolUser.html'
                , tableObj: tableObj_user
                , saveUrl: context + "/api/sysuser/addTea"
                , type: "post"
                , dataType: 'json'
                , htmlData: {schoolCode: '<%=schoolCode%>'}
            })
        });

        //表格内部操作按钮监听
        table.on('tool(mainList_schoolUser)', function (obj) { //注：tool是工具条事件名，mainList_schoolUser是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'update') {
                showDialog({
                    title: '编辑用户 - ' + data.nickname
                    , template:  context + '/views/system/school/template/addSchoolUser.html'
                    , saveUrl: context + '/api/sysuser/update'
                    , tableObj: tableObj_user
                    , htmlData: data
                    , type: "patch"
                    , dataType: 'json'
                })
            }else if (layEvent === 'del') {
                confirmDel(context + '/api/sysuser/del/' + data.user_id, tableObj_user);
            }else if (layEvent === 'role') {
            }else if (layEvent === 'updateDuty') {
                showDialog({
                    title: '修改职位 - ' + data.nickname
                    , template: context + '/views/system/school/template/duty.html'
                    , saveUrl: context + '/api/sysuser/updateDuty'
                    , tableObj: tableObj_user
                    , htmlData: data
                    , type: "PATCH"
                    , dataType: 'json'
                })
            }else if (layEvent === 'status') {
                showDialog({
                    title: '更改用户状态 - ' + data.nickname
                    , template: context + '/views/system/school/template/status.html'
                    , tableObj: tableObj_user
                    , htmlData: data
                    , saveUrl: context + '/api/sysuser/updatestatus'
                    , type: "PATCH"
                    , dataType: 'json'
                })
            }
        });

        function iniForm() {
            $.ajax({
                type: 'get',
                url: context + "/api/sysDuty/list",
                success: function (response) {
                    var data = response.data;
                    console.log(data)
                    if( typeof data == 'object'){
                        var select = $('#dutySelect');
                        select.empty();
                        var t = '<option value="0">全部</option>';
                        for(var i=0,n=data.length;i<n;i++){
                            t += '<option value="'+ data[i].duty_id +'">' + data[i].dutyName +'</option>';
                        }
                        select.append(t);
                        console.log(select)
                        console.log(t)
                        form.render();
                    }
                }
            });
        };
        iniForm();
        // selectSchool();
    });
</script>
<style>
    img.fans-portrait {
        width: 50px;
        height: 50px;
    }

    .fsh-rightPanel .layui-table-body .layui-table-cell {
        height: 50px;
        line-height: 50px;
    }
</style>